package com.gcit.seniorWish.web.modular.enroll.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gcit.seniorWish.core.base.controller.BaseController;
import com.gcit.seniorWish.core.page.PageInfoBT;
import com.gcit.seniorWish.web.common.constant.factory.PageFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.gcit.seniorWish.web.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.gcit.seniorWish.web.common.persistence.model.EnrollSchoolBatchMajor;
import com.gcit.seniorWish.web.modular.enroll.service.IEnrollSchoolBatchMajorService;

import java.util.Map;

/**
 * 报考专业控制器
 *
 * @author fengshuonan
 * @Date 2018-03-19 10:19:38
 */
@Controller
@RequestMapping("/enrollSchoolBatchMajor")
public class EnrollSchoolBatchMajorController extends BaseController {

	private String PREFIX = "/enroll/enrollSchoolBatchMajor";

	@Autowired
	private IEnrollSchoolBatchMajorService enrollSchoolBatchMajorService;

	/**
	 * 跳转到报考专业首页
	 */
	@RequestMapping("/{enrollSchoolId}")
	public String index(@PathVariable String enrollSchoolId, Model model) {
		model.addAttribute("enrollSchoolId", enrollSchoolId);
		return PREFIX + "/enrollSchoolBatchMajor.html";
	}

	/**
	 * 跳转到添加报考专业
	 */
	@RequestMapping("/enrollSchoolBatchMajor_add/{enrollSchoolId}")
	public String enrollSchoolBatchMajorAdd(@PathVariable String enrollSchoolId, Model model) {
		model.addAttribute("enrollSchoolId", enrollSchoolId);
		return PREFIX + "/enrollSchoolBatchMajorAdd.html";
	}

	/**
	 * 跳转到修改报考专业
	 */
	@RequestMapping("/enrollSchoolBatchMajor_update/{enrollSchoolBatchMajorId}")
	public String enrollSchoolBatchMajorUpdate(@PathVariable String enrollSchoolBatchMajorId, Model model) {
		EnrollSchoolBatchMajor enrollSchoolBatchMajor = enrollSchoolBatchMajorService.selectById(enrollSchoolBatchMajorId);
		model.addAttribute("item", enrollSchoolBatchMajor);
		LogObjectHolder.me().set(enrollSchoolBatchMajor);
		return PREFIX + "/enrollSchoolBatchMajorInfo.html";
	}

	/**
	 * 获取报考专业列表
	 */
	@RequestMapping(value = "/list/{enrollSchoolId}")
	@ResponseBody
	public Object list(@PathVariable String enrollSchoolId, String condition) {
		Page page = new PageFactory().defaultPage();
		page.setOrderByField("majorCode") ;
		page.setAsc(true) ;
		EntityWrapper<EnrollSchoolBatchMajor> ew = new EntityWrapper<EnrollSchoolBatchMajor>();
		ew.eq("enroll_school_id", enrollSchoolId);
		if (StrUtil.isNotBlank(condition)) {
			ew.andNew().like("major_code", condition).or().like("major_name", condition);
		}
		return packForBT(enrollSchoolBatchMajorService.selectPage(page, ew));
	}

	/**
	 * 新增报考专业
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add(EnrollSchoolBatchMajor enrollSchoolBatchMajor) {
		enrollSchoolBatchMajorService.insertEnrollMajor(enrollSchoolBatchMajor);
		return SUCCESS_TIP;
	}

	/**
	 * 删除报考专业
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(@RequestParam String enrollSchoolBatchMajorId) {
		enrollSchoolBatchMajorService.deleteMajor(enrollSchoolBatchMajorId);
		return SUCCESS_TIP;
	}

	/**
	 * 修改报考专业
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object update(EnrollSchoolBatchMajor enrollSchoolBatchMajor) {
		enrollSchoolBatchMajorService.updateEnrollMajor(enrollSchoolBatchMajor);
		return SUCCESS_TIP;
	}

	/**
	 * 根据报考学校id查询报考专业列表
	 *
	 * @param enrollSchoolId 报考学校的id
	 * @param majorName 模糊查询的专业名称
	 * @author gaozhichang
	 * @date 2018-03-20 上午 11:24
	 */
	@RequestMapping(value = "/listEnrollMajorForSelect2")
	@ResponseBody
	public Object listEnrollMajorForSelect2(String enrollSchoolId, String majorName) {
		Page page = new PageFactory().defaultPage();
		page.setRecords(enrollSchoolBatchMajorService.listEnrollMajorForSelect2(page, enrollSchoolId, majorName, null));
		return packForSelect2(page);
	}

	/**
	 * 只针对在填报志愿的时候专业查询
	 * 根据报考学校id 和批次类型查询报考专业列表
	 *
	 * @param enrollSchoolId 报考学校的id
	 * @param majorName 模糊查询的专业名称
	 * @author gaozhichang
	 * @date 2018-03-20 上午 11:24
	 */
	@RequestMapping(value = "/listEnrollMajorForSelect22")
	@ResponseBody
	public Object listEnrollMajorForSelect22(String enrollSchoolId, int batchNum) {
		Page page = new PageFactory().defaultPage();
		page.setRecords(enrollSchoolBatchMajorService.listEnrollMajorForSelect22(page, enrollSchoolId,batchNum));
		return packForSelect2(page);
	}
}
